package com.itheima.controller;


import com.itheima.pojo.Result;
import com.itheima.pojo.vo.ArticleDTO;
import com.itheima.pojo.vo.CategoryArticleCountDTO;
import com.itheima.pojo.vo.CommentDTO;
import com.itheima.pojo.vo.MonthlyArticleCountDTO;
import com.itheima.service.DashBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Insight
 * @Description: TODO
 * @Date: 2024-06-11 19:57
 * @Version: 1.0
 */
@RestController
@RequestMapping("/dashboard")
public class DashBoardController {
    @Autowired
    private DashBoardService dashBoardService;

    @GetMapping("/countNewsTotal")
    public Result<Integer> countNewsTotal(){
        return Result.success(dashBoardService.countNewsTotal());
    }
    @GetMapping("/countUsersTotal")
    public Result<Integer> countUsersTotal(){
        return Result.success(dashBoardService.countUsersTotal());
    }
    @GetMapping("/countCommentsTotal")
    public Result<Integer> countCommentsTotal(){
        return Result.success(dashBoardService.countCommentsTotal());
    }
    @GetMapping("/countDaliyCommentsTotal")
    public Result<Integer> countDaliyCommentsTotal(){
        return Result.success(dashBoardService.countDaliyCommentsTotal());
    }

    @GetMapping("/monthlyarticles")
    public Result<Map<String,Object>> getMonthlyArticles() {
        List<MonthlyArticleCountDTO> monthlyArticleCounts = dashBoardService.getMonthlyArticleCounts();
        System.out.println(monthlyArticleCounts);

        List<String> months = new ArrayList<>();
        List<Integer> articleCounts = new ArrayList<>();

        for (MonthlyArticleCountDTO entry : monthlyArticleCounts) {
            months.add(entry.getMonth());
            articleCounts.add(entry.getArticleCount());
        }
        Map<String, Object> response = new HashMap<>();
        response.put("months", months);
        response.put("articleCounts", articleCounts);

        return Result.success(response);
    }

    @GetMapping("/rateArticles")
    public Result<Map<String,Object>> getrateArticles() {
        List<CategoryArticleCountDTO> categoryArticleCountDTOS = dashBoardService.getrateArticles();
        System.out.println(categoryArticleCountDTOS);

        List<String> categorys = new ArrayList<>();
        List<Integer> articles = new ArrayList<>();

        for (CategoryArticleCountDTO entry : categoryArticleCountDTOS) {
            categorys.add(entry.getCategoryName());
            articles.add(entry.getArticleCount());
        }
        Map<String, Object> response = new HashMap<>();
        response.put("categorys", categorys);
        response.put("articles", articles);

        return Result.success(response);
    }

    @GetMapping("/getLatestArticles")
    public Result<List<ArticleDTO>> getLatestArticles() {
        List<ArticleDTO> latestArticles = dashBoardService.getLatestArticles();
        return Result.success(latestArticles);
    }

    @GetMapping("/getLatestComments")
    public Result<List<CommentDTO>> getLatestComments(){
        List<CommentDTO> latestComments = dashBoardService.getLatestComments();
        return Result.success(latestComments);
    }
}
